﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TD.Interface.Controls;

namespace TD.Interface.PopMenus
{
    internal class QuitMenu : PopMenuBase
    {
        private Texture2D titleBar;
        private Texture2D msgBox;

        public QuitMenu(Game1 game) :
            base(game)
        {
            // Do something...
        }

        protected override void LoadContent()
        {
            titleBar = new Texture2D(base.GraphicsDevice, 1, 1);
            titleBar.SetData<Color>(new Color[1] { Color.FromNonPremultiplied(0x00, 0x00, 0x00, 0xFF) });

            msgBox = new Texture2D(base.GraphicsDevice, 1, 1);
            msgBox.SetData<Color>(new Color[1] { Color.FromNonPremultiplied(0x00, 0x00, 0x00, 0xC0) });
            
            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            DisposeTexture(ref titleBar);
            DisposeTexture(ref msgBox);
            
            base.UnloadContent();
        }
        private void DisposeTexture(ref Texture2D texture)
        {
            if (texture != null)
            {
                texture.Dispose();
                texture = null;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (((Game1)Game).InputManager.IsKeyPressed(Keys.Escape))
            {
                MouseClick_Return(null);
            }
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            // The offset on the X- and Y-axis.
            int offsetX = 305;
            int offsetY = 174;

            base.Draw(gameTime);

            // Texture2D
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(this.msgBox, new Rectangle(offsetX, offsetY + 40, 544, 210), Color.White); // Menu background/Information
            spriteBatch.Draw(this.titleBar, new Rectangle(offsetX, offsetY, 544, 40), Color.White); // Title bar
            spriteBatch.End();

            // Font
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.DrawString(base.subFont, "Are you sure you want to quit playing?", new Vector2(offsetX + 4, offsetY + 44), Color.White); // Menu background/Information
            spriteBatch.DrawString(base.font, "Quit", new Vector2(offsetX + 4, offsetY), Color.White); // Title bar
            spriteBatch.End();
        }

        protected override List<DrawableGameComponent> AddItems()
        {
            List<DrawableGameComponent> items = new List<DrawableGameComponent>();
            {
                ButtonConfirm yes = new ButtonConfirm(((Game1)Game), new Point(305, 446), "Yes");
                yes.MouseClick += new ButtonBase.OnClick(this.MouseClick_Exit);
                yes.DrawOrder = (this.DrawOrder + 1);
                items.Add(yes);
            }
            {
                ButtonConfirm no = new ButtonConfirm(((Game1)Game), new Point(582, 446), "No");
                no.MouseClick += new ButtonBase.OnClick(this.MouseClick_Return);
                no.DrawOrder = (this.DrawOrder + 1);
                items.Add(no);
            }
            return items;
        }

        private void MouseClick_Return(object sender)
        {
            ExitMenu();
        }

        private void MouseClick_Exit(object sender)
        {
            ((Game1)Game).Exit();
        }

        protected override void HandleEnabledChanged(object sender, EventArgs e)
        {
            // Do something...
        }

        protected override void HandleVisibleChanged(object sender, EventArgs e)
        {
            // Do something...
        }

        protected override void UpdateItems(List<DrawableGameComponent> items, GameTime gameTime)
        {
            // Do something...
        }
    }
}
